@comment_dir     = "#{@plugindir}/comment";
@comment_flavour = "html";
@recent_comment_count = 10;

def comment
  path = ""

  if @stories.size == 1
    x = Path.new(@stories[0], @datadir)
	 path = [x.path, x.filename].join('/')

    if @cgi.params['mode'][0] == 'comment' then
      if @cgi.params['name'][0] != '' and
         @cgi.params['content'][0] != '' then
        File::open("#{@comment_dir}/#{CGI.escape(path)}","a"){|f|
         f.puts CGI.escapeHTML(@cgi.params['name'][0])
          f.puts CGI.escapeHTML(@cgi.params['addr'][0])
          f.puts Time.now.strftime(@dateformat)
          f.puts CGI.escapeHTML(@cgi.params['content'][0])
          f.puts '--------'
        }

        list = ""
        lc = 1
        begin
          File::open("#{@comment_dir}/recent.list","r"){|f|
            f.each_line do |line|
              line.chomp!
              if !line.nil? and line != ''
                break if lc == @recent_comment_count
                list += "#{line}\n"
                lc += 1
              end
            end
          }
        rescue
        end
        File::open("#{@comment_dir}/recent.list","w"){|f|
          f.print "#{path}\t#{CGI.escapeHTML(@cgi.params['name'][0])}\t"
          f.print "#{Time.now.strftime("%y-%m-%d")}\n"
          f.print list
        }
      end
    end
  end
end

def comment_input
  src =<<"EOS"
<form name="comment" method="POST" action="#{@url}#{@path}/#{@fn}.#{@comment_flavour}">
    <label for="author">Name:</label><br />
	 <input id="author" type="text" name="name" size="30" value="" /><br /><br />
    <label for="addr">URL or E-Mail:</label><br />
	 <input id="addr" type="text" name="addr" size="30" value="" /><br /><br />
    <label for="text">Message:</label><br />
    <textarea id="text" name="content" rows="10" cols="50"></textarea><br />
  <p>
    <input type="hidden" name="mode" value="comment" />
    <input type="submit" value="Submit" /> 
    <input type="reset" value="Clear" />
  </p>
</form>
EOS
  return src
end

def comment_count
  path = "#{@path}/#{@fn}"
  cnt = 0

  begin
    File::open("#{@comment_dir}/#{CGI.escape(path)}","r"){|f|
      while line = f.gets
        line.chomp!
        if line == '--------' then cnt += 1; end
      end
    }
  rescue
  end

  return cnt
end

def comment_disp
  path = "#{@path}/#{@fn}"
  src = ""
  
  begin
    File::open("#{@comment_dir}/#{CGI.escape(path)}","r"){|f|
      while name = f.gets
        name.chomp!
        link = f.gets.chomp!
        time = f.gets.chomp!
        content = ""
        
        while line = f.gets
          line.chomp!
          if line == '--------' then break; end
          content += line + "<br />"
        end
        
        src +=<<EOS
<div class="comment">
EOS
        if link.nil? or link == ''
          src += %(<h5>#{name} (#{time})</h5>)
        else
          src += %(<h5><a href="#{make_link(link)}">#{name}</a> (#{time})</h5>)
        end
        src +=<<EOS
<p>#{content.gsub(/(http:[^<]*)/){|m| %(<a href="#{$1}">#{$1}</a>)}}</p>
</div>
EOS
      end
    }
  rescue
  end

  return src
end

def disp_commentshort
  path = "#{@path}/#{@fn}"
  src =  %(<div class="comment_box">\n)
  src += %(<p><a href="#{@url}#{@path}/#{@fn}.html#comment">comment</a></p>)
  
  begin
    File::open("#{@comment_dir}/#{CGI.escape(path)}","r"){|f|
      while name = f.gets
        name.chomp!
        link = f.gets.chomp!
        content = ""
        
        while line = f.gets
          line.chomp!
          if line == '--------' then break; end
          content += line + "\n"
        end
        
        content = content.split(/\n/)[0]
        
        if link.nil? or link == ''
          src += %(<em>#{name}</em> [#{content}]<br/>\n)
        else
          src += %(<em><a href="#{make_link(link)}">#{name}</a>)
          src += %(</em> [#{content}]<br/>\n)
        end
      end
    }
  rescue
  end

  return src + "</div>"
end

def recent_comment
  src = "<ul>\n"
  begin
    sw = {}
    File::open("#{@comment_dir}/recent.list"){|f|
      f.each_line do |ln|
        ln.chomp!
        if !ln.nil? and ln != ''
          m = ln.split(/\t/)
          src += %(<li><a href="#{@url}#{m[0]}.html#comment">#{m[1]} #{m[2]}</a></li>\n)
        end
      end
    }
    
    src += "</ul>\n"
  rescue
  end
end

def make_link(url)
  if url =~ /(.*)\@(.*)/ then
    return "mailto:#{url}"
  elsif url =~ /http:.*/ then
    return url
  end
  
  return ""
end
